﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmItemReport : Form
    {
        DialogResult dlgResult;
        string _ItemType ="";
        private string keyword = "";
        DataTable _dtItem = null;
        public DataTable dtItem
        {
            get { return _dtItem; }
            set { _dtItem = value; }
        }
        public string ItemType
        {
            get { return _ItemType; }
            set { _ItemType = value; }
        }
        string _PriceType;

        public string PriceType
        {
            get { return _PriceType; }
            set { _PriceType = value; }
        }
        public frmItemReport()
        {
            InitializeComponent();
            DisplayFrame();
            SetHidien();
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        bool CheckValid()
        {
            if (!radAllItem.Checked && !radOneItem.Checked)
            {
                MessageBox.Show("Chưa lựa chọn sản phẩm!", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (radOneItem.Checked && dgvList1.Rows.Count == 0)
            {
                MessageBox.Show("Chưa lựa chọn sản phẩm!", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!radPrice1.Checked && !radPrice2.Checked)
            {
                MessageBox.Show("Chưa lựa chọn loại báo giá!", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        private DataTable Item()
        {
            DataTable dt = new DataTable();
            DataColumn dcol = null;
            dcol = new DataColumn("Item_ID", typeof(int));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("Item_Code", typeof(string));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("Item_Name", typeof(string));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("CalculationUnit_Name", typeof(string));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("Item_WholesalePrice", typeof(decimal));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("Item_RetailPrice", typeof(decimal));
            dt.Columns.Add(dcol);

            int n = dgvList1.Rows.Count;
            if (n > 0)
            {
                for (int i = 0; i <n; i++)
                {
                    DataRow dr = dt.NewRow();

                    dr["Item_ID"] = Convert.ToInt32(dgvList1.Rows[i].Cells["Item_ID"].Value);
                    dr["Item_Code"] = Convert.ToString(dgvList1.Rows[i].Cells["Item_Code"].Value);
                    dr["Item_Name"] = Convert.ToString(dgvList1.Rows[i].Cells["Item_Name"].Value);
                    dr["CalculationUnit_Name"] = Convert.ToString(dgvList1.Rows[i].Cells["CalculationUnit_Name"].Value);
                    dr["Item_WholesalePrice"] = Convert.ToDecimal(dgvList1.Rows[i].Cells["Item_WholesalePrice"].Value);
                    dr["Item_RetailPrice"] = Convert.ToDecimal(dgvList1.Rows[i].Cells["Item_RetailPrice"].Value);
                    dt.Rows.InsertAt(dr, i);
                    
                }
                return dt;
            }
            else
            {
                return null;
            }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
                return;
            if (radOneItem.Checked)
            {
                _ItemType = "SP DA CHON";
                dtItem = Item();
            }
            if (radPrice1.Checked)
                _PriceType = "BAO GIA BAN BUON";
            else
                _PriceType = "BAO GIA BAN LE";
            dlgResult = DialogResult.OK;
            this.Close();
        }

        private void frmItemReport_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }



        private void frmItemReport_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            dlgResult = DialogResult.Cancel;
            this.Close();
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Item_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", 35, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_Code", "Mã HH", "", 120, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_Name", "Tên HH", "", 180, true, true);
            UISetting.AddColumn(ref dgvList1, "CalculationUnit_Name", "ĐVT", "C", 90, true, false);
            UISetting.AddColumn(ref dgvList1, "Item_WholesalePrice", "Giá buôn", "R", 90, true, false);
            UISetting.AddColumn(ref dgvList1, "Item_RetailPrice", "Giá lẻ", "R", 90, true, false);
            
            DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "Lệnh";
            buttonColumn.Name = "Control";
            buttonColumn.Text = "Xóa";
            buttonColumn.Width = 60;
            buttonColumn.UseColumnTextForButtonValue = true;
            dgvList1.Columns.Add(buttonColumn);

            dgvList1.CellClick += new DataGridViewCellEventHandler(dgvList1_CellClick);
        }
        void dgvList1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Kiểm tra xóa rows
            if (e.RowIndex < 0 || e.ColumnIndex !=
                    dgvList1.Columns["Control"].Index) return;
            dgvList1.Rows.RemoveAt(e.RowIndex);
            //Tính lại tổng tiền sau khi xóa
            //Sắp xếp lại thứ tự
            for (int i = 0; i < dgvList1.Rows.Count; i++)
            {
                dgvList1.Rows[i].Cells["RowNumber"].Value = i + 1;
            }
        }

        private void radAllItem_CheckedChanged(object sender, EventArgs e)
        {
            SetHidien();
        }

        private void radOneItem_CheckedChanged(object sender, EventArgs e)
        {
            SetHidien();
        }
        private void SetHidien()
        {
            if (radAllItem.Checked)
            {
                btnSelectItems.Enabled = false;
                dgvList1.Enabled = false;
                dgvList1.Rows.Clear();
            }
            else if (radOneItem.Checked)
            {
                btnSelectItems.Enabled = true;
                dgvList1.Enabled = true;
            }
            else
            {
                btnSelectItems.Enabled = false;
                dgvList1.Enabled = false;
                dgvList1.Rows.Clear();
            }
        }

        private void btnSelectItems_Click(object sender, EventArgs e)
        {
            frmItemList f = new frmItemList(true, keyword);
            f.ShowDialog(this);
            int n = dgvList1.Rows.Count;

            if (f.Item_ID > 0)
            {
                //Kiểm tra xem trong danh sách đã có mặt hàng này chưa. Nếu có thì ko cho add nữa
                for (int i = 0; i < n; i++)
                {
                    if (f.Item_ID == Convert.ToInt32(dgvList1.Rows[i].Cells["Item_ID"].Value))
                    {
                        return;
                    }
                }
                //Sau khi kiểm tra xong, nếu được làm tiếp thì sẽ add vào danh sách như sau:
                dgvList1.Rows.Insert(n, 1);
                dgvList1.Rows[n].Cells["RowNumber"].Value = n + 1;
                dgvList1.Rows[n].Cells["Item_ID"].Value = f.Item_ID;
                dgvList1.Rows[n].Cells["Item_Code"].Value = f.Item_Code;
                dgvList1.Rows[n].Cells["Item_Name"].Value = f.Item_Name;
                dgvList1.Rows[n].Cells["CalculationUnit_Name"].Value = f.Unit_Name;
                dgvList1.Rows[n].Cells["Item_WholesalePrice"].Value = f.Item_WholesalePrice.ToString("#,##0");
                dgvList1.Rows[n].Cells["Item_RetailPrice"].Value = f.Item_PurchasePrice.ToString("#,##0");
                keyword = f.TuKhoa;
            }
        }

    }
}